export default {
  // 必须得在引入该组件的地方增加 tableRefName 和 idKey
  data() {
    return {
      multipleSelectionAll: [], // 所有页面选中的数据
      removeIds: [], // 清除的id
      clearAll: false, // 是否操作清除全部
      orderData: []
    };
  },
  created() {
    if (!this.tableRefName || (!this.idKey && this.idKey !== 0)) {
      console.error('tableRefName 或 idKey 未设置');
    }
  },
  methods: {
    selectTable(selection, row) {
      const idKey = this.idKey;
      let isSelect; // 判断当前是选中还是取消选中
      if (row) {
        // 单选
        const id = row[idKey];
        isSelect = selection.some(i => i[idKey] === id);
        if (isSelect) {
          const temp = this.removeIds.filter(item => {
            return item !== row.id;
          });
          this.removeIds = [...new Set(temp)];
          this.selectionNum = this.orderData.length - this.removeIds.length;
        }
      } else {
        // 多选按钮
        isSelect = selection.length > 0;
      }
      if (isSelect) {
        this.clearAll = false;
        this.addSelect(selection, row);
      } else {
        this.removeSelect(selection, row);
      }
      if (this.tableSelectionChange) this.tableSelectionChange(this.multipleSelectionAll);
    },
    addSelect(selection, row) {
      // 找出removeIds中selection选中的id,并移除
      const tempIds = this.removeIds.filter(item => {
        return !selection.some(cur => {
          return cur.id === item;
        });
      });
      this.removeIds = tempIds;
      this.selectionNum = this.orderData.length - this.removeIds.length;
      if (!this.multipleSelectionAll || this.multipleSelectionAll.length === 0) {
        this.multipleSelectionAll = selection;
        return;
      }
      const selectIds = this.multipleSelectionAll.map(i => i[this.idKey]);
      // 全选勾选时，需要减去已经勾选的
      const concatOrder = selection.filter(i => !selectIds.includes(i[this.idKey]));
      this.multipleSelectionAll = this.multipleSelectionAll.concat(concatOrder);
    },
    removeSelect(selection, row) {
      const idKey = this.idKey;
      const tableData = this.$refs[this.tableRefName || 'tableRef'] && this.$refs[this.tableRefName || 'tableRef'].data || [];
      const currentPageSelectIds = selection.map(i => i[idKey]);
      const removeIds = [];
      tableData.forEach(i => {
        if (!currentPageSelectIds.includes(i[idKey])) {
          removeIds.push(i[idKey]);
        }
      });
      const temp = this.removeIds.concat(removeIds);
      this.removeIds = [...new Set(temp)];
      this.selectionNum = this.orderData.length - this.removeIds.length;
      this.multipleSelectionAll = this.multipleSelectionAll.filter(i => {
        return !removeIds.includes(i[idKey]);
      });
      if (!selection.length && this.curTabName === 'selected' && this.selectedPage > 1) {
        this.selectedPage = this.selectedPage - 1;
      }
    },
    clearMixinSelection() {
      this.$nextTick(() => {
        this.multipleSelectionAll = [];
        if (this.$refs[this.tableRefName || 'tableRef']) this.$refs[this.tableRefName || 'tableRef'].clearSelection();
        if (this.tableSelectionChange) this.tableSelectionChange(this.multipleSelectionAll);
      });
    },
    setSelectRow(tableData, refName) { // 翻页时自动选中
      this.$nextTick(() => {
        if (!this.multipleSelectionAll || this.multipleSelectionAll.length <= 0) {
          return;
        }
        // 标识当前行的唯一键的名称
        const idKey = this.idKey;
        const selectAllIds = this.multipleSelectionAll.map(row => row[idKey]);
        // 关联流水弹窗列表取不到外面的ref
        let ref;
        if (refName) {
          ref = this.$refs[this.tableRefName].$refs[refName];
        } else {
          ref = this.$refs[this.tableRefName || 'tableRef'];
        }
        for (let i = 0; i < tableData.length; i++) {
          if (selectAllIds.indexOf(tableData[i][idKey]) >= 0) {
            if (this.isUmyTable) {
              ref.toggleRowSelection([{ row: tableData[i], selected: true }]);
            } else {
              ref.toggleRowSelection(tableData[i], true);
            }
          }
        }
        /* if (this.$refs[this.tableRefName || 'tableRef']) this.$refs[this.tableRefName || 'tableRef'].clearSelection();
        for (let i = 0; i < tableData.length; i++) {
          if (selectAllIds.indexOf(tableData[i][idKey]) >= 0) {
            if (this.isUmyTable) {
              this.$refs[this.tableRefName || 'tableRef'].toggleRowSelection([{ row: tableData[i], selected: true }]);
            } else {
              this.$refs[this.tableRefName || 'tableRef'].toggleRowSelection(tableData[i], true);
            }
          }
        } */
      });
    },
    tableRowClassName({ row, rowIndex }, extendClass = '') {
      if (this.multipleSelectionAll && this.multipleSelectionAll.length > 0) {
        const ids = this.multipleSelectionAll.map(i => i[this.idKey]);
        return ids.includes(row[this.idKey]) ? `table-selected ${extendClass}` : extendClass;
      }
      return extendClass;
    },
    // 审批单删除后取消对应审批单的勾选
    resetItemSelected(id, idKey = 'id') {
      this.multipleSelectionAll = this.multipleSelectionAll.filter(i => i[idKey] !== id);
    }
  }
};
